# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

package require tcltest 2
namespace import tcltest::*

source "testlib.tcl"

test unlink_outside_sandbox "Test that unlink(2) does not work outside of the sandbox" \
    -setup {
        exec -- touch unlinkme
        [setup [list deny /]]
    } \
    -cleanup {
        file delete -force unlinkme
        [expect [list "$cwd/unlinkme"]]
    } \
    -body {exec -ignorestderr -- ./unlink unlinkme 2>@1} \
    -result "unlink: No such file or directory"

test unlink_inside_sandbox "Test that unlink(2) works inside the sandbox" \
    -setup {
        exec -- touch unlinkme
        [setup [list allow /]]
    } \
    -cleanup {
        file delete -force unlinkme
        [expect {}]
    } \
    -body {exec -ignorestderr -- ./unlink unlinkme 2>@1} \
    -result ""

test unlink_uninitialized "Test that unlink(2) works outside the sandbox if darwintrace is uninitialized" \
    -setup {
        exec -- touch unlinkme
        [setup [list deny /]]
    } \
    -cleanup {
        file delete -force unlinkme
        [expect {}]
    } \
    -body {
        set ::env(DARWINTRACE_UNINITIALIZE) 1
        set output [exec -ignorestderr -- ./unlink unlinkme 2>@1]
        unset ::env(DARWINTRACE_UNINITIALIZE)
        return $output
    } \
    -result ""

cleanupTests
